﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Windows.Forms;
using Moonlite.Platform.Project;
using Moonlite.ExtensionApi;
using Moonlite.ExtensionApi.GlobalInterfaces;

namespace Moonlite.UI.Shell.Controls {
    public partial class ProjectProperties : Form {
        private static List<UserControl> customPages = new List<UserControl>();
        private List<string> sessionDeletedPaths = new List<string>();
        private List<string> SessionDeletedPaths {
            get { return sessionDeletedPaths; }
        }

        private IProject project;
        public IProject Project {
            get { return project; }
            set {
                project = value;
                if (project != null) {
                    UpdateProperties();
                }
            }
        }

        public static void AddPropertyPage(UserControl control) {
            customPages.Add(control);
        }

        public static void RemovePropertyPage(UserControl control) {
            if (customPages.Contains(control)) {
                customPages.Remove(control);
            }
        }

        private void UpdateReferences(IProject project) {
            references.Items.Clear();
            references.Items.AddRange(project.References.ToArray());
        }

        private void UpdateProperties(IProject project) {
            path.Text = project.Path;
            mapName.Text = project.Name;
            descriptionEdit.Text = project.Description;
            category.Text = project.Type == ProjectType.Jass ? "Warcraft 3" : "Starcraft 2";
            UpdateReferences(project);
        }

        private void UpdateProperties() {
            UpdateProperties(Project);
        }

        private void SaveProperties(IProject project) {
            project.Description = descriptionEdit.Text;
            project.References.Clear();
            DirectoryHelper.DeleteDirectory(Project.GetReferencesFolderPath());
            DirectoryHelper.CreateFolderPath(Project.GetReferencesFolderPath());
            foreach (object item in references.Items) {
                project.References.Add((string)item);
                string path = String.Format("{0}\\{1}", Project.GetReferencesFolderPath(), Path.GetFileName((string)item));
                File.Copy((string)item, path);
            }

            SessionDeletedPaths.ForEach(path => {
                if (File.Exists(path) == true)
                    File.Delete(path);
            });

            ProjectSerializer.SaveProject(project);
        }

        private void SaveProperties() {
            SaveProperties(Project);
        }

        private void OnResetClick(object sender, EventArgs e) {
            DialogResult dialogResult = MessageBox.Show("Are you sure you want to reset to project's initial values?",
                "Moonlite Map Studio", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1);

            if (dialogResult != DialogResult.Yes)
                return;

            UpdateProperties();
        }

        private void OnApplyClick(object sender, EventArgs e) {
            SaveProperties();
        }

        private void OnReferencePathButtonClick(object sender, EventArgs e) {
            using (OpenFileDialog openFileDialog = new OpenFileDialog { Filter = "Compiled Library Files (*.cml)|*.cml" }) {
                if (openFileDialog.ShowDialog() == DialogResult.OK) {
                    referencePathTextBox.Text = openFileDialog.FileName;
                }
            }
        }

        private void OnUpdateClick(object sender, EventArgs e) {
            UpdateReferences(Project);
        }

        private void OnAddReferenceClick(object sender, EventArgs e) {
            if (references.Items.Contains(referencePathTextBox.Text)) {
                MessageBox.Show("A reference to that library has already been added.", "Moonlite Map Studio", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
                return;
            }

            if (File.Exists(referencePathTextBox.Text) == true) {
                references.Items.Add(referencePathTextBox.Text);
            }
            else {
                MessageBox.Show("Library file doesn't exist.", "Moonlite Map Studio", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
            }
        }

        private void OnReferencePathTextChanged(object sender, EventArgs e) {
            addReference.Enabled = File.Exists(referencePathTextBox.Text) == true ? true : false;
        }

        private void OnReferencesKeyUp(object sender, KeyEventArgs e) {
            if (e.KeyCode != Keys.Delete || references.SelectedItem == null)
                return;

            SessionDeletedPaths.Add((string)references.SelectedItem);
            references.Items.Remove(references.SelectedItem);
        }

        public ProjectProperties() {
            InitializeComponent();
            foreach (UserControl control in customPages) {
                TabPage page = new TabPage { Text = control.ToString() };
                page.Controls.Add(control);
                xtraTabControl.TabPages.Add(page);
            }
        }
    }
}